﻿using AutoMapper;
using EFEntity;
using ISerives;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Serives
{
    public class config_majorServices : Iconfig_majorServices
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public config_majorServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }
        public async Task<int> Add(config_major s)
        {
            myDbContext.ZWSZ.Add(s);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public async Task<int> Delete(int id)
        {
            config_major ef = new config_major()
            {
                mak_id = id
            };
            myDbContext.ZWSZ.Attach(ef);
            myDbContext.ZWSZ.Remove(ef);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public string idd()
        {
            string str = myDbContext.ZWSZ.Select(e => e.major_id).Max();
            string[] num = Regex.Split(str, "");
            int n = int.Parse(num[num.Length - 2]);
            string id = "";
            if (n == 18)
            {
                num[num.Length - 2] = "0";
                int j = int.Parse(num[num.Length - 3]);
                num[num.Length - 3] = j.ToString();
            }
            else
            {
                num[num.Length - 2] = (n + 1).ToString();
            }
            for (int i = 0; i < num.Length; i++)
            {
                id += num[i];
            }
            return id;
        }

        public async Task<List<Mconfig_major>> Select()
        {
            List<Mconfig_major> list1 = new List<Mconfig_major>();
            await Task.Run(() =>
            {
                List<config_major> list = myDbContext.ZWSZ.ToList();
                list1 = mapper.Map <List<Mconfig_major>>(list);
                
            });
            return list1;
        }

        public async Task<List<config_major>> Selecter(string id)
        {
            List<config_major> list = new List<config_major>();
            await Task.Run(() =>
            {
                list = myDbContext.ZWSZ.Where(e => e.major_kind_id == id).ToList();
            });
            return list;
        }
    }
}
